package service.impl;

import com.mysql.cj.Session;
import dao.IOrderDao;
import dao.IShopDao;
import dao.ITradeDao;
import dao.IUserDao;
import model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.IShopService;
import util.DateUtil;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.math.BigDecimal;
import java.rmi.MarshalException;
import java.sql.Date;
import java.util.List;
@Service
public class ShopService implements IShopService {
    @Autowired
    private IShopDao iShopDao;
    @Autowired
    private IUserDao iUserDao;
    @Autowired
    private IOrderDao iOrderDao;
    @Autowired
    private ITradeDao iTradeDao;
    @Override
    public List<Goods> selectGoods(Goods goods) {
        return iShopDao.selectGoods(goods);
    }

    @Override
    public List<Goods> selectParentId(Goods goods) {
        return  iShopDao.selectParentId(goods);
    }

    @Override
    public List<Goods> selectSortId(Goods goods) {
        return  iShopDao.selectSortId(goods);
    }

    @Override
    public List<Order> selectOrder(Order order) {
        return  iShopDao.selectOrder(order);
    }

    @Override
    public String insertCart(Cart cart, Goods goods) {
        System.out.println("商品id="+goods.getId()+";商品库存="+goods.getgCount());
        int count = iShopDao.insertCart(cart);
		int count1 = iShopDao.updateCount(goods);
        if(count >0 && count1 >0){
            return  "1";
        }else{
            return  "0";
        }
    }

    @Override
    public ModelAndView selectCart(Cart cart, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User m = (User) session.getAttribute("user");
        List<Goods> list = iShopDao.selectCart(cart);
        int count = iShopDao.count(cart);
        ModelAndView mv = new ModelAndView();
        mv.addObject("list", list);
        mv.addObject("user", m);
        mv.addObject("count", count);
        mv.setViewName("shop/cart");
        return  mv;
    }

    @Override
    public ModelAndView order(Goods goods, HttpServletRequest request , String orderNo) {
        System.out.println("----------------------");
        System.out.println("orderNo="+orderNo);
        Order order = new Order();
        order.setOrderNo(orderNo);
        ModelAndView mv = new ModelAndView();
        HttpSession session = request.getSession();
        User m = (User) session.getAttribute("user");
        Goods good = iShopDao.selectGoods(goods).get(0);
        good.setBuyCount(goods.getBuyCount());
        mv.addObject("user", m);
        mv.addObject("good", good);
        mv.addObject("order", order);
        mv.setViewName("shop/place_order");

        return  mv;
    }

    @Override
    @Transactional//开启事务
    public String order(Order order, Goods goods) {
        System.out.println("用户id是！！！！"+order.getuId());
        System.out.println("订单价格："+order.getOrderMoney());
        User id = new User();
        id.setId(order.getuId());
        User u = iUserDao.login(id).get(0);
        order.setuId(u.getId());
        order.setgId((goods.getId()));
        order.setgPrice(goods.getgPrice());
        order.setTotalMoney(order.getOrderMoney());
        System.out.println("------------------------------");
        System.out.println(u.getId());
        String phone = u.getuPhone();
        order.setOrderNo(DateUtil.dateFormat()+phone.substring(7,11));
        order.setCreateTime( DateUtil.currentDate());
        System.out.println(order.getOrderNo());
        System.out.println("------------------------------");
        int count = iShopDao.insertOrder(order);
        int count1 = iShopDao.updateCount(goods);
        int count2 = iOrderDao.orderGoods(order);
        if(count <1 || count1 <1 || count2 < 1){
            return  "0";
        }else if (count >=1 && count1 >=1 && count2 >=1){
            return  order.getOrderNo();
        }else{
            return  "0";
        }

    }

    @Override
    public String deleteCart(Integer id) {
        System.out.println("-------------------------");
        System.out.println(id);
        System.out.println("--------------------------");

        int count = iShopDao.deleteCrat(id);
        System.out.println(count);
        if(count <1){
            return  "0";
        }else{
            return  "1";
        }
    }

    @Override
    public String pay(Integer id, String pass, String orderNo) {
        System.out.println("用户id="+id+"， 用户密码="+pass+"， 订单号="+orderNo);
        User user = new User();
        user.setId(id);
        User m = iUserDao.login(user).get(0);
        BigDecimal num = new BigDecimal(m.getuMoney().toString());
        if(m.getuPass().equals(pass)){
            Order order = new Order();
            order.setOrderNo(orderNo);
            Order o = iShopDao.selectOrder(order).get(0);
            BigDecimal count = o.getOrderMoney();
            if(num.compareTo(count)== 1 ){
                m.setuMoney(num.subtract(count));
                int a = iUserDao.updateMoney(m);
                if(a <1){
                    return "0";//更新用户余额失败;
                }else{
                    int b = iOrderDao.orderStatus(o);
                    if(b<1){
                        return "4";//更新订单状态失败
                    }else{
                        Trade trade = new Trade();
                        trade.setCreateTime(DateUtil.currentDate());
                        trade.setTradeType(2);
                        trade.setOrderNo(DateUtil.dateFormat()+m.getuPhone().substring(7,11));
                        trade.setTradeMoney(count);
                        trade.setuId(m.getId());
                        int c = iTradeDao.insertTrade(trade);
                        if(c <1){
                            return "5";//生成流水失败
                        }else{
                            return "1";
                        }
                    }

                }

            }else{
                return  "2";//余额不足
            }
        }else{
            return  "3";//支付密码错误
        }

    }

    @Override
    public ModelAndView listBySort(Goods goods, HttpServletRequest request) {
        List<Goods> list = iShopDao.selectGoods(goods);
        HttpSession session = request.getSession();
        User m = (User)session.getAttribute("user");
        ModelAndView mv = new ModelAndView();
        //商品一级分类
        List<Goods> parent = iShopDao.selectParentId(null);
        mv.addObject("parent", parent);
        //商品二级分类
        List<Goods> sort = iShopDao.selectSortId(null);
        mv.addObject("sort", sort);
        mv.addObject("list", list);
        mv.addObject("user", m);
        mv.setViewName("shop/sortIndex");
        return  mv;
    }

    @Override
    public ModelAndView listByParent(Goods goods, HttpServletRequest request) {
        List<Goods> list = iShopDao.selectGoods(goods);
        HttpSession session = request.getSession();
        User m = (User)session.getAttribute("user");
        ModelAndView mv = new ModelAndView();
        //商品一级分类
        List<Goods> parent = iShopDao.selectParentId(null);
        mv.addObject("parent", parent);
        //商品二级分类
        List<Goods> sort = iShopDao.selectSortId(null);
        mv.addObject("sort", sort);
        mv.addObject("list", list);
        mv.addObject("user", m);
        mv.setViewName("shop/parentIndex");
        return  mv;
    }


}
